Conversation
Reviewer's GuideIntroduces a greedy two-pass algorithm to distribute candies: initialize a count array with 1s, perform a left-to-right pass to handle ascending ratings, a right-to-left pass to adjust for descending ratings, then sum the results. Class diagram for the new Solution class implementing candy distributionclassDiagram
class Solution {
+int candy(vector<int>& ratings)
}
Flow diagram for the candy distribution algorithmflowchart TD
A["Start: ratings array"] --> B["Initialize count array with 1s"]
B --> C["Left to Right: for i = 1 to n-1"]
C --> D["If ratings[i] > ratings[i-1], count[i] = count[i-1] + 1"]
D --> E["Right to Left: for i = n-2 down to 0"]
E --> F["If ratings[i] > ratings[i+1], count[i] = max(count[i], count[i+1] + 1)"]
F --> G["Sum count array for total candies"]
G --> H["Return total candies"]
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Closed
4 tasks
SjxSubham
approved these changes
Oct 29, 2025
Owner
|
@kaif969 Star the repo as well... |
🎉 Congrats on getting your PR merged in, @kaif969! 🙌🏼Thanks for your contribution every effort helps improve the project. Looking forward to seeing more from you! 🥳✨ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

PR Title Format: 135. candy.cpp
Intuition
We are given ratings of children standing in a line, and we must distribute candies such that:
The key idea is to traverse the ratings twice:
This ensures both sides of each child are considered.
Approach
Initialize a
countarray of sizen(number of children), with all values set to1. This satisfies the minimum candy condition.Left to Right Traversal:
i = 1ton - 1.ratings[i] > ratings[i - 1], setcount[i] = count[i - 1] + 1.i = n - 2down to0.ratings[i] > ratings[i + 1], setcount[i] = max(count[i], count[i + 1] + 1)to satisfy both directions.countarray to get the minimum total candies.Code Solution (C++)
Related Issues
#229
By submitting this PR, I confirm that:
Summary by Sourcery
New Features:
Summary by Sourcery
New Features: